import { createRouter, createWebHistory } from 'vue-router'
import Cookies from 'js-cookie'
const routes = [
  {
    path: '/login',
    name: 'Login-Router',
    component: () => import('@/view/login/Login'),
  },
  {
    path: '/register',
    name: 'Register-Router',
    component: () => import('@/view/login/Register'),
  },
  {
    path: '/',
    name: 'Index-Router',
    redirect: '/videoList',
    component: () => import('@/view/Index'),
    children: [
      {
        path: 'videoList',
        name: 'VideoList-Router',
        component: () => import('@/view/video/VideoList'),
      },
      {
        path: 'videoDetail/:id',
        name: 'VideoDetail-Router',
        component: () => import('@/view/video/VideoDetail'),
      },
      {
        path: 'video-class',
        name: 'VideoClass-Router',
        props: (route) => ({
          name: route.query.name,
          type: route.query.type,
          order: route.query.order,
          currentPage: route.query.currentPage,
        }),
        component: () => import('@/view/video/VideoClassify'),
      },
      {
        path: 'user-index',
        name: 'UserIndex-Router',
        meta: {
          authorization: true,
        },
        component: () => import('@/view/user/UserIndex'),
        redirect: '/user-index/user-info',
        children: [
          {
            path: 'user-info',
            name: 'UserInfo-Router',
            meta: {
              authorization: true,
              index: 0,
            },
            component: () => import('@/view/user/UserInfo'),
          },
          {
            path: 'user-avatar',
            name: 'UserAvatar-Router',
            meta: {
              authorization: true,
              index: 1,
            },
            component: () => import('@/view/user/UserAvatar'),
          },
          {
            path: 'user-message',
            name: 'UserMessage-Router',
            meta: {
              authorization: true,
              index: 2,
            },
            component: () => import('@/view/user/UserMessage'),
          },
          {
            path: 'user-cart',
            name: 'UserCart-Router',
            meta: {
              authorization: true,
              index: 3,
            },
            component: () => import('@/view/user/UserCart'),
          },
          {
            path: 'user-recode',
            name: 'UserRecode-Router',
            meta: {
              authorization: true,
              index: 4,
            },
            component: () => import('@/view/user/UserRecode'),
          },
          {
            path: 'user-pass',
            name: 'UserPass-Router',
            meta: {
              authorization: true,
              index: 5,
            },
            component: () => import('@/view/user/UserPassWord'),
          },
          {
            path: 'publish-video',
            name: 'PublishVideo-Router',
            meta: {
              authorization: true,
              index: 6,
            },
            component: () => import('@/view/user/PublishVideo'),
          },
        ],
      },
    ],
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {
  if (to.meta.authorization) {
    if (Cookies.get('authorization')) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }, // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})

export default router
